﻿using NetHelperCore;
using SharpConfig;

namespace BLL
{
    public class ConfigBLL
    {
        public static ConfigModel Config;


        /// <summary>
        /// 日志数据库信息
        /// </summary>
        public static DBConfigModel LogDBConfig;

        /// <summary>
        /// 启动子程序信息
        /// </summary>
        public static List<ProgramInfo> ProgramInfoList;

        /// <summary>
        /// 管理服务信息
        /// </summary>
        public static List<ServiceInfo> ServiceInfoList;

        public static string ConfigFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + System.Diagnostics.Process.GetCurrentProcess().ProcessName + @"Config.ini";


        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="configPath"></param>
        public static bool LoadConfig(string configPath = "")
        {
            if (!string.IsNullOrWhiteSpace(configPath))
            {
                ConfigFilePath = configPath;
            }

            string _configValue = string.Empty;

            if (File.Exists(ConfigFilePath))
            {
                _configValue = File.ReadAllText(ConfigFilePath);
            }

            if (string.IsNullOrWhiteSpace(_configValue))
            {
                Config = new ConfigModel();
                LogDBConfig = new DBConfigModel();
                ProgramInfoList = new List<ProgramInfo>();
                ServiceInfoList = new List<ServiceInfo>();
                SaveConfig();
                return false;
            }
            else
            {
                Configuration _cfg = Configuration.LoadFromString(_configValue);
                Config = _cfg["Base"].ToObject<ConfigModel>();
                LogDBConfig = _cfg["LogDB"].ToObject<DBConfigModel>();
                ProgramInfoList = JsonHelper.GetObjectFromJsonStr<List<ProgramInfo>>(Config.ProgramList);
                ServiceInfoList = JsonHelper.GetObjectFromJsonStr<List<ServiceInfo>>(Config.ServiceList);

                _cfg = new Configuration();
                _cfg.Add(Section.FromObject("Base", Config), "基本配置");
                _cfg.Add(Section.FromObject("LogDB", LogDBConfig), "日志数据库配置");


                if (_configValue != _cfg.StringRepresentation)
                {
                    SaveConfig();
                }
                return true;
            }


        }

        /// <summary>
        /// 保持配置文件
        /// </summary>
        public static void SaveConfig()
        {
            if (string.IsNullOrWhiteSpace(ConfigFilePath))
            {
                return;
            }

            if (Config == null)
            {
                Config = new ConfigModel();
            }

            if (File.Exists(ConfigFilePath))
            {
                File.Delete(ConfigFilePath);
            }

            if (Config == null)
            {
                Config = new ConfigModel();
            }

            if (LogDBConfig == null)
            {
                LogDBConfig = new DBConfigModel();
                LogDBConfig.DBPath = NetHelperCore.FileHelper.GetRunPath() + @"\log";
            }

            Config.ProgramList = JsonHelper.GetJsonStrFromObject(ProgramInfoList, false);
            Config.ServiceList = JsonHelper.GetJsonStrFromObject(ServiceInfoList, false);

            Configuration _cfg = new Configuration();
            _cfg.Add(Section.FromObject("Base", Config), "基本配置");
            _cfg.Add(Section.FromObject("LogDB", LogDBConfig), "日志数据库配置");

            _cfg.SaveToFile(ConfigFilePath);
        }


    }

    /// <summary>
    /// 基础配置类
    /// </summary>
    public class ConfigModel
    {
        /// <summary>
        /// 本服务监听端口号
        /// </summary>
        [SharpConfig.Comment("本服务监听端口号")]
        public int[] ServicePort { get; set; } = { 22000 };

        /// <summary>
        /// 是否有前置代理，服务器替换IP
        /// </summary>
        [SharpConfig.Comment("是否有前置代理，服务器替换IP")]
        public bool IsProxyReplaceIP { get; set; } = false;

        /// <summary>
        /// 是否写入跟踪日志
        /// </summary>
        [SharpConfig.Comment("是否写入跟踪日志")]
        public bool WriteLog { get; set; } = true;

        /// <summary>
        /// 如果写入日志，写入日志最低级别，1，写入错误日志  2,写入警告以上  3，写入跟踪日志以上
        /// </summary>
        [SharpConfig.Comment("如果写入日志，写入日志最低级别，1，写入错误日志  2,写入警告以上  3，写入跟踪日志以上")]
        public int WriteLogType { get; set; } = 1;

        /// <summary>
        /// Windows服务名称
        /// </summary>
        [SharpConfig.Comment("Windows服务名称")]
        public string WindowsServiceName { get; set; } = "JL-ServiceManage";

        /// <summary>
        /// Windows服务说明
        /// </summary>
        [SharpConfig.Comment("Windows服务说明")]
        public string WindowsServiceDescription { get; set; } = "服务管理器";

        /// <summary>
        /// 子程序信息列表
        /// </summary>
        [SharpConfig.Comment("需启动的子程序信息")]
        public string ProgramList { get; set; }

        /// <summary>
        /// 服务信息列表
        /// </summary>
        [SharpConfig.Comment("需启动的服务信息")]
        public string ServiceList { get; set; }

    }

    /// <summary>
    /// 子程序信息
    /// </summary>
    public class ProgramInfo
    {
        /// <summary>
        /// 编号
        /// </summary>
        public long ID { get; set; } = 0;

        /// <summary>
        /// 路径
        /// </summary>
        public string FilePath { get; set; } = "";

        /// <summary>
        /// 页面端口号
        /// </summary>
        public int Port { get; set; } = 0;
    }

    public class ServiceInfo
    {
        /// <summary>
        /// 编号
        /// </summary>
        public long ID { get; set; } = 0;

        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = "";

        /// <summary>
        /// 服务备注
        /// </summary>
        public string ServiceDescription { get; set; } = "";

        /// <summary>
        /// 路径
        /// </summary>
        public string FilePath { get; set; } = "";

        /// <summary>
        /// 页面端口号
        /// </summary>
        public int Port { get; set; } = 0;
    }


    /// <summary>
    /// 请求日志数据库信息配置
    /// </summary>
    public class DBConfigModel
    {
        /// <summary>
        /// 是否记录客户端访问日志
        /// </summary>
        [SharpConfig.Comment("是否记录客户端访问日志")]
        public bool IsRequestLog { get; set; } = true;

        /// <summary>
        /// 是否记录请求结果内容
        /// </summary>
        [SharpConfig.Comment("是否记录请求结果内容")]
        public bool IsResponseLog { get; set; } = true;

        /// <summary>
        /// 记录请求结果内容最大长度
        /// </summary>
        [SharpConfig.Comment("记录请求结果内容最大长度")]
        public int MaxResponseLog { get; set; } = 300;

        /// <summary>
        /// 数据库类型 SQLLite，SQLServer，MySQL
        /// </summary>
        [SharpConfig.Comment("数据库类型 SQLite，SQLServer，MySQL")]
        public string DBType { get; set; } = "SQLite";

        /// <summary>
        /// SQLLite数据库存放文件夹
        /// </summary>
        [SharpConfig.Comment("SQLite数据库存放文件夹")]
        public string DBPath { get; set; } = "";

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        [SharpConfig.Comment("数据库连接字符串")]
        public string ConnectionString { get; set; } = "";
    }

}
